#include <iostream>
#include <pthread.h>
#include <string.h>
#include <unistd.h>

using namespace std;

void *threadRoutine(void *args)
{
    pthread_detach(pthread_self());//线程自己分离自己，如果线程已经是joinable状态就不会执行分离
    string name = static_cast<const char *>(args);
    int cnt = 5;
    while (cnt)
    {
        cout << name << ":" << cnt-- << endl;
        sleep(1);
    }
    return nullptr;
}

int main()
{
    pthread_t tid;
    pthread_create(&tid, nullptr, threadRoutine, (void *)"thread 1");
    //pthread_detach(tid);//将线程进行分离，一个线程分离后不能立刻join
    int n = pthread_join(tid, nullptr);
    if (n != 0)
    {
        cerr << "error:" << n << ":" << strerror(n) << endl;
    }
    sleep(10);
    return 0;
}